package j.framework.qmd.supported;

import j.framework.qmd.core.DataType;
import j.util.StringUtils;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;

/**
 * 关于实体类的一些辅助方法
 * User: jason
 * Date: 14-9-2 上午10:21
 */
public class EntityUtils {
    public static DataType fromDbDataTypeToJavaType(ResultSetMetaData rsmd,int i)throws SQLException{
        String dbColumnTypeName = rsmd.getColumnTypeName(i);
        if(StringUtils.isNullOrEmpty(dbColumnTypeName)){
            return DataType.STRING;
        }
        String t = dbColumnTypeName.toUpperCase();
        DataType dt = DataType.STRING;
        if(t.equals("CHAR")){
            dt = DataType.STRING;
        }else if(t.equals("VARCHAR")){
            dt = DataType.STRING;
        }else if(t.equals("TEXT")){
            dt = DataType.STRING;
        }else if(t.equals("INT")){
            dt = DataType.INTEGER;
        }else if(t.equals("INTEGER")){
            dt = DataType.INTEGER;
        }else if(t.equals("TINYINT")){
            dt = DataType.BOOLEAN;
        }else if(t.equals("BIGINT")){
            dt = DataType.LONG;
        }else if(t.equals("DATETIME")){
            dt = DataType.DATETIME;
        } else if(t.equals("DATE")){
            dt = DataType.DATE;
        }else if(t.equals("TIME")){
            dt = DataType.TIME;
        }else if(t.equals("FLOAT")){
            dt = DataType.FLOAT;
        }else if(t.equals("DOUBLE")){
            dt = DataType.DOUBLE;
        }else if(t.equals("DECIMAL")){
            int scale = rsmd.getScale(i);
            if(scale == 0){
                dt = DataType.LONG;
            }else if(scale>=6){
                dt = DataType.DOUBLE;
            }else{
                dt = DataType.FLOAT;
            }
        }else if(t.equals("NUMERIC")){
            int scale = rsmd.getScale(i);
            if(scale == 0){
                dt = DataType.LONG;
            }else if(scale>=6){
                dt = DataType.DOUBLE;
            }else{
                dt = DataType.FLOAT;
            }
        }
        return dt;
    }
}
